﻿#region namespaces

using System;
using System.Collections.Concurrent;
using System.Reflection;

#endregion

namespace ZOIL.DataModel
{
    /// <summary>
    /// Basic implementation originating from: http://stackoverflow.com/a/1825569
    /// </summary>
    public static class TypeFinder
    {
        private static readonly ConcurrentDictionary<string, Type> TypeCache = new ConcurrentDictionary<string, Type>();

        public static bool TryFindType(string fullName, out Type t)
        {
            if (!TypeCache.TryGetValue(fullName, out t))
            {
                foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
                {
                    t = a.GetType(fullName);
                    if (t != null)
                    {
                        TypeCache[fullName] = t;
                        break;
                    }
                }
            }
            return t != null;
        }
    }
}